********************************************************************************
* RD Analysis
********************************************************************************
gl out "${output}/forPaper"

clear all
set obs 200
gen pe = .
gen pese = .
gen plot_ctry = ""
gen index = ""

tempfile main
save `main', replace

loc i = 0
graph drop _all
qui foreach s in  _nw { 
	qui foreach c in  kenyaipush  rwanda colombia  kenyaklps nepal { // 
		loc ++ i
		noi di "----------------------------------------------------"
		noi di "------                    `c'                   ----"
		noi di "----------------------------------------------------"
		use "${output}\clean\stringency_cases_clean.dta", clear
		keep if ctrytext == "`c'"
		if "`c'" == "kenyaipush" | "`c'" == "rwanda" {
			replace days_post_maxChange = days_post_maxChange - 7
			*replace days_post_maxChange = . if abs(days_post_maxChange) < 5
		}
		*net install rdrobust, from(https://raw.githubusercontent.com/rdpackages/rdrobust/master/stata) replace
		egen mdepression = mean(depression), by(pid)
		bysort pid (days_post_maxChange): gen dep_base = ___depression[1]
	
		gen moy = month(date)
		recode moy (1 2 = 1 "Jan-Feb") (3 4 = 2 "Mar-April") (5 6 = 3 "May-June") ///
			(7 8 = 4 "July-Aug") (9 10 = 5 "Sept-Oct") (11 12 = 6 "Nov-Dec"), gen(mcat)
			tab mcat, gen(mcat)
		if "`c'" == "nepal"  noi eststo `c'`s': rdrobust ___depression`s' ///
			days_post_maxChange , masspoints(adjust) covs(dep_base)
		if "`c'" == "kenyaipush" noi eststo `c'`s': rdrobust ___depression`s' ///
			days_post_maxChange , masspoints(adjust) h(75) covs(dep_base)

		if "`c'" == "rwanda" noi eststo `c'`s': rdrobust ___depression`s' days_post_maxChange , masspoints(adjust) ///
			covs(dep_base) h(500) b(500) 
		if "`c'" == "kenyaklps" noi eststo `c'`s': rdrobust ___depression`s' days_post_maxChange ///
			, masspoints(adjust) h(900) 
		if "`c'" == "colombia" noi eststo `c'`s': rdrobust ___depression`s' ///
			days_post_maxChange , masspoints(off) h(755) 
			
		
		preserve
		use `main', clear
			replace pe = _b[RD_Estimate] if _n == `i'
			replace pese = _se[RD_Estimate] if _n == `i'
			replace plot_ctry = "`c'" if _n == `i'
			replace index = "`s'" if _n == `i'
			noi codebook pe pese plot_ctry index
		save `main', replace
		restore

		loc h `e(h_l)' `e(h_r)'
		if "`c'" == "kenyaklps" loc h 600 200
		if "`c'" == "colombia" loc h 600 300
		if "`c'" == "kenyaipush" loc h 75 75
		rdplot ___depression`s' days_post_maxChange if -e(h_l)< days_post_maxChange & days_post_maxChange <= e(h_r), ///
			   binselect(esmv) kernel(triangular)  h(`h') p(1) covs(`e(covs)')  ///
			   graph_options(title() ///
							 ytitle(Mental Health) ///
							 xtitle(Days post onset of lockdown) ///
							 graphregion(color(white)) name(`c'`s')) 
		graph export "${out}/FigS6/rd_`c'`s'.png", replace	
	}
}
use `main', clear
metaan pe pese if index == "_nw", label(plot_ctry) dl forest
matrix b = r(eff)
matrix V = r(effvar)
matrix rownames b = RD_Estimate
matrix colnames b = RD_Estimate
matrix rownames V = RD_Estimate
matrix colnames V = RD_Estimate
ereturn post b V
eststo meta_nw


*esttab meta_nw colombia_nw kenyaipush_nw kenyaklps_nw  nepal_nw rwanda_nw using "${out}/RD_full_nw.tex", ///
*					keep(*) booktabs replace label  wrap p star(* .1 ** .05 *** .01) ///
*					title(RD Estimates from Select Countries)  ///
*					mtitles("RE Aggregate" "COL" "KEN I" "KEN K"  "NPL" "RWA" ) //					
esttab meta_nw colombia_nw kenyaipush_nw kenyaklps_nw  nepal_nw rwanda_nw using "${out}/Table2.tex", ///
					keep(*) booktabs replace label  wrap p star(* .1 ** .05 *** .01) ///
					title(RD Estimates from Select Countries)  ///
					mtitles("RE Aggregate" "COL" "KEN3" "KEN1"  "NPL" "RWA" ) //		




clear all
set obs 500
gen pe = .
gen pese = .
gen plot_ctry = ""
gen index = ""
gen bw = .
tempfile main
save `main'

loc i = 0
foreach s in _nw {
qui foreach c in nepal kenyaipush  rwanda colombia  kenyaklps  {
foreach p of numlist  0.3(0.05)1.0 {
	loc ++ i 

	use "${output}\clean\stringency_cases_clean.dta", clear
		keep if ctrytext == "`c'"
		if "`c'" == "kenyaipush" | "`c'" == "rwanda" {
			replace days_post_maxChange = days_post_maxChange - 7
			*replace days_post_maxChange = . if abs(days_post_maxChange) < 5
		}
		*net install rdrobust, from(https://raw.githubusercontent.com/rdpackages/rdrobust/master/stata) replace
		egen mdepression = mean(depression), by(pid)
		bysort pid (days_post_maxChange): gen dep_base = ___depression[1]
		*if "`c'" == "kenyaipush" keep if round != 0
	
		*bysort pid (days_post_maxChange): keep if _N > 1
		gen moy = month(date)
		recode moy (1 2 = 1 "Jan-Feb") (3 4 = 2 "Mar-April") (5 6 = 3 "May-June") ///
			(7 8 = 4 "July-Aug") (9 10 = 5 "Sept-Oct") (11 12 = 6 "Nov-Dec"), gen(mcat)
			tab mcat, gen(mcat)
			
			
			sum days_post_maxChange if !mi(___depression`s') & days_post_maxChange < 0
	loc h = abs(`r(max)' - (`r(max)' - `r(min)')*`p')
			
			noi di "`c' : `p'  : `h' "
	if regexm("`c'","(nepal|kenyaipush|rwanda)")  eststo `c'`s': rdrobust ___depression`s' ///
			days_post_maxChange , masspoints(adjust) covs(dep_base) h(`h')
		
		if "`c'" == "kenyaklps"  eststo `c'`s': rdrobust ___depression`s' days_post_maxChange ///
			, masspoints(adjust) h(`h') 
		if "`c'" == "colombia"  eststo `c'`s': rdrobust ___depression`s' ///
			days_post_maxChange , masspoints(off) h(`h') 
			

			
	preserve
		use `main', clear
			replace pe = _b[RD_Estimate] if _n == `i'
			replace pese = _se[RD_Estimate] if _n == `i'
			replace plot_ctry = "`c'" if _n == `i'
			replace index = "`s'" if _n == `i'
			replace bw = `p' if _n == `i'
			 codebook pe pese plot_ctry index
		save `main', replace
		restore
}	
}
}
	use `main', clear


loc i = 480
foreach p of numlist 0.3(0.05)1.0 {
	loc ++ i
	di `p'
	count if bw == `p'
	tab bw
	metaan pe pese if round(bw, 0.01) == round(`p', 0.01), label(plot_ctry) reml forest
	replace pe = `r(eff)' if _n == `i'
			replace pese = sqrt(`r(effvar)') if _n == `i'
			replace plot_ctry = "Aggregate" if _n == `i'
			replace bw = `p' if _n == `i'
}
		replace plot_ctry = "NPL" if plot_ctry == "nepal"
	replace plot_ctry = "RWA" if plot_ctry == "rwanda"
	replace plot_ctry = "KEN1" if plot_ctry == "kenyaklps"
	replace plot_ctry = "KEN3" if plot_ctry == "kenyaipush"
	replace plot_ctry = "COL" if plot_ctry == "colombia"
	gen pe_hi = pe + pese*1.96
	gen pe_lo = pe - pese*1.96
	
	preserve
	keep if plot_ctry != "Aggregate"
tw (line pe bw , color(gs0)) (line pe_hi bw, color(gs0) lpattern(dash)) ///
	(line pe_lo bw, color(gs0) lpattern(dash)), ///
		by(plot_ctry, graphregion(color(white)) plotregion(margin(sides)) note("")) ///
		xtitle(Share of Available Time Period Used) ylabel(, angle(0)) ///
		legend(  order(1 "Estimate" 2 "95% CI")) 
graph export "${out}/FigS5/RD_sensitivity_all.png", replace
restore

preserve
keep if plot_ctry == "Aggregate"
tw (line pe bw , color(gs0)) (line pe_hi bw, color(gs0) lpattern(dash)) (line pe_lo bw, color(gs0) lpattern(dash)),  xtitle(Share of Available Time Period Used) graphregion(color(white)) ylabel(, angle(0))  legend(  order(1 "Estimate" 2 "95% CI"))
graph export "${out}/FigS5/RD_sensitivity_agg.png", replace

restore
